﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace TEdit
{
    public partial class Preferences : Form
    {
        public Preferences()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Instance = InstanceChkbox.Checked;
            Properties.Settings.Default.Putty = textBox1.Text;
            Properties.Settings.Default.Save();
            Properties.Settings.Default.Reload();
            this.Close();
        }

        private void Preferences_Load(object sender, EventArgs e)
        {
            InstanceChkbox.Checked = Properties.Settings.Default.Instance;
            textBox1.Text = Properties.Settings.Default.Putty;
        }

        private void btnAddContextMenu_Click(object sender, EventArgs e)
        {
            try
            {
                string strLoc = Properties.Settings.Default.TEdit;
                Microsoft.Win32.RegistryKey key;
                key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("*\\shell", true);
                key.CreateSubKey("Open with TEdit");
                key.Flush();
                key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("*\\shell\\Open with TEdit", true);
                key.CreateSubKey("Command");
                key.Flush();
                key = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("*\\shell\\Open with TEdit\\Command", true);
                key.SetValue("", strLoc + " %1");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnRemoveContextMenu_Click(object sender, EventArgs e)
        {
            Microsoft.Win32.Registry.ClassesRoot.DeleteSubKeyTree("*\\shell\\Open with TEdit");
        }

        private void btnFileBroswer_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf=new OpenFileDialog();
            opf.Filter = "Exe files (*.exe)|*.exe";
            if (opf.ShowDialog() == DialogResult.OK)
                textBox1.Text = opf.FileName;
        }
    }
}
